//
//  SearchViewController.m
//  Tospur-text
//
//  Created by Tospur_hewei on 15/9/29.
//  Copyright (c) 2015年 Tospur_hewei. All rights reserved.
//

#import "SearchViewController.h"
#import <AFNetworking.h>
#import "Encryption.h"
#import <MBProgressHUD.h>
#import "SearchModel.h"
#import <UIImageView+WebCache.h>
#import "BuildDetailTableViewController.h"
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property (nonatomic,strong) MBProgressHUD *HUDS;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) SearchModel *model;
@property (nonatomic,strong) UISearchBar *searchBar;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    self.modelArray = [NSMutableArray array];
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 375, 50)];
    self.searchBar.showsCancelButton = NO;
    self.searchBar.delegate = self;
    [self.view addSubview:self.searchBar];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 375, 667-80-64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.view.backgroundColor = [UIColor clearColor];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    self.model = _modelArray[indexPath.row];
    cell.textLabel.text = _model.GardenName;
    NSURL *url = [NSURL URLWithString:_model.GardenPic];
    [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"楼.jpg"]];
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BuildDetailTableViewController *buildVC = [[BuildDetailTableViewController alloc]init];
    buildVC.model = self.model;
    buildVC.modelArray = self.modelArray;
    [self.navigationController pushViewController:buildVC animated:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self updateSearchResultsForSearchBar:searchBar];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsCancelButton = YES;
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    searchBar.showsCancelButton = NO;
    [searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
     searchBar.showsCancelButton = NO;
     [searchBar resignFirstResponder];
}
- (void)updateSearchResultsForSearchBar:(UISearchBar *)searchBar
{

    if (searchBar.text.length != 0) {
        //self.HUDS = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        //self.HUDS.labelText=@"正在加载...";
        AFHTTPRequestOperationManager *tx = [AFHTTPRequestOperationManager manager];
        //申明返回的结果是json类型
        tx.responseSerializer = [AFJSONResponseSerializer serializer];
        //申明请求的数据是json类型
        tx.requestSerializer = [AFJSONRequestSerializer serializer];
        // [tx.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
        NSString *urlStr = @"http://172.20.5.142:7001/api/GetGardenList";
        NSString *signature = [[Encryption shareInstance]sha1];
        NSLog(@"+++++ %@",signature);
        NSString *str = [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSLog(@"UTF-8 ==== %@",str);
        NSDictionary *dict = @{
                               @"appkey": @"tospur123",
                               @"signature": signature,
                               @"GardenName": str,
                               //@"OrderbyColumn": @"AvgPr",
                               @"PageSize": @"10",
                               };
        [tx POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSLog(@"%@",responseObject);
            
            NSArray *arr = [responseObject objectForKey:@"GardenList"];
            self.modelArray = [NSMutableArray array];
            for (NSMutableDictionary *dic in arr) {
                self.model = [[SearchModel alloc]init];
                _model.GardenName = dic[@"GardenName"];
                _model.GardenPic = dic[@"GardenPic"];
                [self.modelArray addObject:_model];
                NSLog( @"数组有值吗=== %@",_modelArray);
            }
            [self.tableView reloadData];
            //[_HUDS hide:YES afterDelay:1];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
            
        }];
    }
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
